/**
 * Created with IntelliJ IDEA.
 *
 * @author: cxs
 * @date: 2023/6/28
 * @time: 11:54
 * @className：CarServiceIml
 * @description：车辆管理Service实现类
 */
package com.bailan.bus.service.impl;

import com.bailan.bus.domain.Car;
import com.bailan.bus.mapper.CarMapper;
import com.bailan.bus.service.ICarService;
import com.bailan.bus.vo.CarVo;
import com.bailan.common.util.AppFileUtils;
import com.bailan.common.util.DataGridView;
import com.bailan.common.util.SysConstant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarServiceIml implements ICarService {
    @Autowired
    CarMapper carMapper;
    /**
     * 模糊查询显示车辆
     *
     * @param carVo
     * @return
     */
    @Override
    public DataGridView queryAllCar(CarVo carVo) {
        Page<Object> page = PageHelper.startPage(carVo.getPage(), carVo.getLimit());
        List<Car> data=this.carMapper.queryAllCar(carVo);
        System.out.println(carVo.getIsrenting()+"\n\n\n\n\n\n");
        return new DataGridView(page.getTotal(),data);
    }

    /**
     * 根据车牌号删除车辆信息
     *
     * @param carnumber
     */
    @Override
    public void deleteCarByCarNumber(String carnumber) {
        /**
         *删除磁盘中的图片
         */
        Car car=this.carMapper.selectByPrimaryKey(carnumber);
        //如果不是默认图片就删除
        if (!car.getCarimg().equals(SysConstant.DEFAULT_CAR_IMG)){
            AppFileUtils.deleteFileUsePath(car.getCarimg());
        }
        this.carMapper.deleteByCarNumber(carnumber);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleteCars(String[] ids) {
        for (String id:ids){
            this.deleteCarByCarNumber(id);
        }
    }

    /**
     * 修改车辆信息
     *
     * @param carVo
     */
    @Override
    public void updateCar(CarVo carVo) {
        this.carMapper.updateCarByCarName(carVo);
    }

    /**
     * 添加新车辆
     *
     * @param carVo
     */
    @Override
    public void addNewCar(CarVo carVo) {
        this.carMapper.addOneNewCar(carVo);
    }

    @Override
    public Car queryCarByCarNumber(String carnumber) {
        return  this.carMapper.selectByPrimaryKey(carnumber);
    }

}
